package com.fzsf.drawingboard.utils;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

import com.fzsf.drawingboard.app.MyApplication;
import com.fzsf.drawingboard.bluetooth.SimpleFZWritingPadCallBackListener;
import com.fzsf.drawingboard.bluetooth.iskn.FZWritingPad_ISKNHelper;
import com.fzsf.drawingboard.bluetooth.royole.FZWritingPad_RYHelper;

/**
 * Created by Taijl on 2017/9/22.
 */

public class BlueToothUtils {
    /**
     * Bluetooth 设备可见时间，单位：秒。
     */
    public static final int BLUETOOTH_DISCOVERABLE_DURATION = 300;

    /**
     * 检测设备是否支持蓝牙
     *
     * @return
     */
    public static boolean isBleSupport() {
        return MyApplication.getInstance().getPackageManager().hasSystemFeature("android.hardware.bluetooth_le");
    }

    /**
     * 蓝牙是否打开（可用）
     *
     * @return
     */
    public static boolean isBlueToothEnable() {
        BluetoothManager bluetoothManager = (BluetoothManager) MyApplication.getInstance().getSystemService(Context.BLUETOOTH_SERVICE);
        return bluetoothManager.getAdapter().isEnabled();
    }

    /**
     * GPS 是否可用
     * 从6.0开始 使用蓝牙还要求有GSP
     *
     * @return
     */
    public static boolean isGpsEnable() {
        LocationManager locationManager
                = (LocationManager) MyApplication.getInstance().getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }

    /**
     * 去打开GPS
     *
     * @param activity    页面（上下文）
     * @param requestCode （请求码）
     */
    public static void turnOnGps(Activity activity, int requestCode) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 打开蓝颜
     *
     * @param activity    页面（上下文）
     * @param requestCode （请求码）
     */
    public static void turnOnBluetooth(Activity activity, int requestCode) {
        // 请求打开 Bluetooth
        Intent requestBluetoothOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        // 设置 Bluetooth 设备可以被其它 Bluetooth 设备扫描到
        requestBluetoothOn.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        // 设置 Bluetooth 设备可见时间
        requestBluetoothOn.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BLUETOOTH_DISCOVERABLE_DURATION);
        // 请求开启 Bluetooth
        activity.startActivityForResult(requestBluetoothOn, requestCode);
    }

    /**
     * 进行扫描蓝牙扫描设备（使用柔宇的SDK）
     *
     * @param listener
     */
    public static void startScan(SimpleFZWritingPadCallBackListener listener) {
//        FZWritingPad_RYHelper.getPadHelper().setSimpleListener(listener);
//        FZWritingPad_RYHelper.getPadHelper().startScan();
        FZWritingPad_ISKNHelper.getInstance().setFZWritingPadCallBackListener(listener);
        FZWritingPad_ISKNHelper.getInstance().startScan();
    }

    /**
     * 停止蓝牙扫描
     */
    public static void stopScan() {
        FZWritingPad_RYHelper.getPadHelper().stopScan();
    }

    /**
     * 判断是否是柔宇的设备
     *
     * @param deviceName 设备名称
     * @return
     */
    public static boolean isRoyoleDevice(String deviceName) {
        return StringUtils.isNotEmpty(deviceName) && deviceName.startsWith("FZ085") || StringUtils.isNotEmpty(deviceName) && deviceName.startsWith("Royole");
    }

    /**
     * 是否是磐度的板子
     *
     * @param deviceName
     * @return
     */
    public static boolean isHypenDevice(String deviceName) {
        return StringUtils.isNotEmpty(deviceName) && deviceName.startsWith("FO");
    }

    /**
     * 是否是磐度的板子
     *
     * @param deviceName
     * @return
     */
    public static boolean isIsknDevice(String deviceName) {
        return StringUtils.isNotEmpty(deviceName) && deviceName.contains("Slate");
    }
}
